package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysScenicArea;
import com.mars.module.admin.entity.SysScenicSpot;
import com.mars.module.admin.entity.SysTravelRouteDetails;
import com.mars.module.admin.mapper.SysScenicAreaMapper;
import com.mars.module.admin.mapper.SysScenicSpotMapper;
import com.mars.module.admin.mapper.SysTravelRouteDetailsMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysTravelRouteRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysTravelRouteMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysTravelRoute;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysTravelRouteService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 旅游路线业务层处理
 *
 * @author mars
 * @date 2024-03-26
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysTravelRouteServiceImpl implements ISysTravelRouteService {

    private final SysTravelRouteMapper baseMapper;
    private final SysTravelRouteDetailsMapper detailsMapper;
    private final SysScenicSpotMapper spotMapper;
    private final SysScenicAreaMapper areaMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysTravelRoute add(SysTravelRouteRequest request) {
        SysTravelRoute entity = SysTravelRoute.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysTravelRouteRequest request) {
        SysTravelRoute entity = SysTravelRoute.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysTravelRoute getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysTravelRoute> pageList(SysTravelRouteRequest request) {
        Page<SysTravelRoute> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysTravelRoute> query = this.buildWrapper(request);
        IPage<SysTravelRoute> pageRecord = baseMapper.selectPage(page, query);
        List<SysTravelRoute> records = pageRecord.getRecords();
        List<SysTravelRoute> recordsNew = new ArrayList<>();
        for (SysTravelRoute route : records) {
            List<SysTravelRouteDetails> detailsList = detailsMapper.selectList(new QueryWrapper<SysTravelRouteDetails>().eq("route_id", route.getId()));
            detailsList.forEach(e -> {
                SysScenicSpot scenicSpot = spotMapper.selectById(e.getSpotId());
                e.setSpotName(scenicSpot == null ? "" : scenicSpot.getName());
            });
            route.setDetailsList(detailsList);
            SysScenicArea area = areaMapper.selectById(route.getAreaId());
            route.setAreaName(area == null ? "" : area.getName());
            recordsNew.add(route);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysTravelRoute> list(SysTravelRouteRequest request) {
        LambdaQueryWrapper<SysTravelRoute> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysTravelRoute> buildWrapper(SysTravelRouteRequest param) {
        LambdaQueryWrapper<SysTravelRoute> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysTravelRoute::getName, param.getName());
        }
        if (StringUtils.isNotBlank(param.getIntroduce())) {
            query.like(SysTravelRoute::getIntroduce, param.getIntroduce());
        }
        if (param.getAreaId() != null) {
            query.eq(SysTravelRoute::getAreaId, param.getAreaId());
        }
        return query;
    }

}
